package com.ruoyi.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.dto.ZtfAppMainTablesDTO;
import com.ruoyi.system.mapper.ZtfAppCodeNcMapper;
import com.ruoyi.system.mapper.ZtfAppMainTablesMapper;
import com.ruoyi.system.mapper.ZtfVideoTableMapper;
import com.ruoyi.system.result.PageResult;
import com.ruoyi.system.service.IZtfAppMainTablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 智提分考试大师Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-30
 */
@Service
public class ZtfAppMainTablesServiceImpl implements IZtfAppMainTablesService
{
    @Autowired
    private ZtfAppMainTablesMapper ztfAppMainTablesMapper;

    @Autowired
    private ZtfVideoTableMapper ztfVideoTableMapper;

    @Autowired
    private ZtfAppCodeNcMapper ztfAppCodeNcMapper;

    /**
     * 查询智提分考试大师
     * 
     * @param ztfid 智提分考试大师主键
     * @return 智提分考试大师
     */
    @Override
    public ZtfMainTables selectZtfMainTablesByZtfid(Long ztfid)
    {
        return ztfAppMainTablesMapper.selectZtfMainTablesByZtfid(ztfid);
    }

    /**
     * 查询智提分考试大师列表
     * 
     * @param ztfAppMainTables 智提分考试大师（系统分页）
     * @return 智提分考试大师
     */
    @Override
    public List<ZtfAppMainTables> selectZtfMainTablesList(long userId,ZtfAppMainTables ztfAppMainTables)
    {

        List<ZtfAppMainTables> ztfMainTablesList= ztfAppMainTablesMapper.selectZtfMainTablesList(userId,ztfAppMainTables);
        if(ztfMainTablesList!=null && ztfMainTablesList.size()>0){
            for (ZtfAppMainTables ztfMainTables1 : ztfMainTablesList) {
                List<ZtfAppVideoTable> videoList = ztfMainTables1.getVideoList();
                if(videoList!=null&& videoList.size()>0){
                    Integer totalOver=videoList.stream().mapToInt(ZtfAppVideoTable::getIsOver).sum();
                    ztfMainTables1.setZtfStudyClasshort(Long.valueOf(totalOver));
                }

            }
        }
        return ztfMainTablesList;
    }

    /**
     * 新增智提分考试大师
     * 
     * @param ztfMainTables 智提分考试大师
     * @return 结果
     */
    @Override
    @Transactional
    public int insertZtfMainTables(ZtfMainTables ztfMainTables)
    {
       //获取相关视频数据
        List<ZtfVideoTable> videoTableList = ztfMainTables.getVideoList();

        //设置图片名称
        ztfMainTables.setZtfPicName(ztfMainTables.getZtfPicName());
        //用流处理视频数据
        videoTableList.stream().forEach(ztfVideoTable -> {
            //生成视频id
            String uuid = IdUtils.simpleUUID();
            ztfMainTables.setZtfVideoId(uuid);
            ZtfVideoTable ztfVideoTable1=new ZtfVideoTable();
            ztfVideoTable1.setVideoUrl(ztfVideoTable.getFileUrl());
            ztfVideoTable1.setUploadTime(new Date());
            ztfVideoTable1.setTitle(ztfVideoTable.getVideoName());
            ztfVideoTable1.setId(uuid);
            ztfVideoTable1.setZtfId("");
            //新增视频相关数据
            ztfVideoTableMapper.insertZtfVideoTable(ztfVideoTable1);
        });
        return ztfAppMainTablesMapper.insertZtfMainTables(ztfMainTables);
    }

    /**
     * 修改智提分考试大师
     * 
     * @param ztfMainTables 智提分考试大师
     * @return 结果
     */
    @Override
    public int updateZtfMainTables(ZtfMainTables ztfMainTables)
    {
        //从前端获取数据
        List<ZtfVideoTable> videoList = ztfMainTables.getVideoList();
        for (ZtfVideoTable ztfVideoTable : videoList) {
            ZtfVideoTable ztfVideoTable1=new ZtfVideoTable();
            if(ztfVideoTable.getId()==null){
                //生成视频id
                String uuid = IdUtils.simpleUUID();
                ztfVideoTable1.setVideoUrl(ztfVideoTable.getFileUrl());
                ztfVideoTable1.setUploadTime(new Date());
                ztfVideoTable1.setTitle(ztfVideoTable.getVideoName());
                ztfVideoTable1.setId(uuid);
                ztfVideoTable1.setZtfId("");
                //添加分类（查询条件）
                ztfVideoTable1.setZtfName(ztfMainTables.getZtfName());
                ztfVideoTableMapper.insertZtfVideoTable(ztfVideoTable1);
            }

        }
        return ztfAppMainTablesMapper.updateZtfMainTables(ztfMainTables);
    }

    /**
     * 批量删除智提分考试大师
     * 
     * @param ztfids 需要删除的智提分考试大师主键
     * @return 结果
     */
    @Override
    public int deleteZtfMainTablesByZtfids(String[] ztfids)
    {
        return ztfAppMainTablesMapper.deleteZtfMainTablesByZtfids(ztfids);
    }

    /**
     * 删除智提分考试大师信息
     * 
     * @param ztfid 智提分考试大师主键
     * @return 结果
     */
    @Override
    public int deleteZtfMainTablesByZtfid(Long ztfid)
    {
        return ztfAppMainTablesMapper.deleteZtfMainTablesByZtfid(ztfid);
    }

    /**
     * 新的分页
     * @param ztfAppMainTablesDTO
     * @return
     */
    @Override
    public PageResult newSelectZtfMainTablesList(ZtfAppMainTablesDTO ztfAppMainTablesDTO, long userId) {
        PageHelper.startPage(ztfAppMainTablesDTO.getPage(), ztfAppMainTablesDTO.getPageSize());
        //该插件的规则 需要返回一个page对象
        Page<ZtfAppMainTables> page =  ztfAppMainTablesMapper.pageSelect(ztfAppMainTablesDTO);

        //查询主表用于显示分页总数
//        Page<ZtfMainTables> mainTablesPage =  ztfAppMainTablesMapper.SelectByZtfMainTable();
        //调用PageHelper中的方法获取总条数和数据
        long total = page.getTotal();

        int pageNum=page.getPageNum();

        int pageSize=page.getPageSize();
        List<ZtfAppMainTables> ztfMainTablesList = page.getResult();

        if(ztfMainTablesList.size()>0){
            for (ZtfAppMainTables ztfAppMainTables : ztfMainTablesList) {
                //根据分类id查询激活码状态
               ZtfAppCodeNc ztfAppCodeNc=ztfAppCodeNcMapper.selectCodeByMianId(ztfAppMainTables.getZtfName(),userId);
               if(ztfAppCodeNc!=null){
                   ztfAppMainTables.setZtfCodeSort(ztfAppCodeNc.getStatus());
                   List<ZtfAppVideoTable> videoList = ztfAppMainTables.getVideoList();
                   //用stream流求和
                   Integer totalOver=videoList.stream().mapToInt(ZtfAppVideoTable::getIsOver).sum();

                   ztfAppMainTables.setZtfStudyClasshort(Long.valueOf(totalOver));

               }
               //处理激活码状态数据
                long codeSort=ztfAppMainTables.getZtfCodeSort()==null? 0: ztfAppMainTables.getZtfCodeSort();
                ztfAppMainTables.setZtfCodeSort(codeSort);
            }
        }

        return new PageResult(total,ztfMainTablesList,pageNum,pageSize);
    }
}
